<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/shadow-dom.js"></script>
<div id="targets">
    <div>Non-shadow</div>

    <div>
        <template>Shadow</template>
    </div>

    <div>
        <template>
            <div>
                <template>Multi-level</template>
            </div>
        </template>
    </div>

    <details></details>
</div>
<script>
convertTemplatesToShadowRootsWithin(targets);

for (var i = 0; i < targets.children.length; ++i) {
    var target = targets.children[i];
    test(function () {
        testCaretRangeFromPoint(target);
    }, target.tagName);
}

function testCaretRangeFromPoint(target) {
    var x = target.offsetLeft;
    var y = target.offsetTop + target.offsetHeight / 2;
    for (var offsetX = 2; offsetX >= -2; --offsetX) {
        var range = document.caretRangeFromPoint(x + offsetX, y);
        assert_true(isInDocumentTree(range.startContainer), "isInDocumentTree(startContainer) when offsetX=" + offsetX);
        assert_true(isInDocumentTree(range.endContainer), "isInDocumentTree(endContainer) when offsetX=" + offsetX);
    }
}

function isInDocumentTree(node) {
    return rootNode(node) === document;
}

function rootNode(node) {
    for (;;) {
        var parent = node.parentNode;
        if (!parent)
            return node;
        node = parent;
    }
}

if (window.testRunner)
    targets.style.display = "none";
</script>
